package com.wutuo.roster.provider.dao;

import com.wutuo.roster.api.request.ChildApplyRequest;
import com.wutuo.roster.api.request.ListChildApplyRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 孩子申请 dao sql 层
 *
 * @author lvh
 * @version 1.0
 */

public class ChildApplySqlProvider extends BaseChildApplySqlProvider {

    public String lisForPage(ListChildApplyRequest request) {

        BEGIN();
        getSelected();
        FROM("child_apply a ");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        sql.append(" order by a.create_date desc ");
        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }
        return sql.toString();
    }

    public String list(ChildApplyRequest request) {

        BEGIN();
        getSelected();
        FROM("child_apply a");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {

        SELECT(
            "a.grade as classes,a.id,user_id as userId,user_type as userType,cm_institution_id as cmInstitutionId,create_by as createBy,create_date as createDate,update_by as updateBy,update_date as updateDate,remarks as remarks,del_flag as delFlag,name as name,photo as photo,sex as sex,tg_type as tgType,grade as grade,year as year,state as state");
    }

    public void getComonJoin(ChildApplyRequest request) {

        if (request.getCmInstitutionId() != null) {
            SqlBuilder.WHERE(" a.cm_institution_id = #{cmInstitutionId,jdbcType=BIGINT}");
        }

        if (!StringUtils.isEmpty(request.getState())) {
            SqlBuilder.WHERE(" a.state = #{state}");
        }

        if (!StringUtils.isEmpty(request.getName())) {
            SqlBuilder.WHERE(" a.name = #{name}");
        }

    }
}
